package com.team622.shop.user.service.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team622.shop.user.model.dto.MemberInfoPageQueryDTO;
import com.team622.shop.user.model.entity.MemberInfo;
import com.team622.shop.user.model.vo.MemberInfoPageVO;
import com.team622.shop.user.model.vo.MemberInfoVO;
import com.team622.shop.user.service.handlers.BaseContextHandler;
import com.team622.shop.user.service.mapper.MemberInfoMapper;
import com.team622.shop.user.service.service.MemberInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author team622
 * @since 2025-07-15
 */
@Slf4j
@Service
public class MemberInfoServiceImpl extends ServiceImpl<MemberInfoMapper, MemberInfo> implements MemberInfoService {

    /**
     * 获取当前登录用户信息
     * 从请求头中获取token，解析出用户ID，然后查询用户信息
     *
     * @return 当前登录用户的信息
     */
    @Override
    public MemberInfoVO info() {
        Long id = BaseContextHandler.get();
        log.info("id:{}", id);
        // 根据用户ID查询用户信息
        MemberInfo memberInfo = this.getById(id);
        MemberInfoVO memberInfoVO = new MemberInfoVO();
        BeanUtils.copyProperties(memberInfo, memberInfoVO);
        String mobile = memberInfo.getMobile();
        mobile = mobile.substring(0, 3) + "****" + mobile.substring(7);
        memberInfoVO.setMobile(mobile);
        return memberInfoVO;
    }

    @Override
    public IPage<MemberInfoPageVO> getPage(MemberInfoPageQueryDTO memberPageQueryDTO) {
        // 创建分页对象
        IPage<MemberInfoPageVO> page = new Page<>(memberPageQueryDTO.getCurrent(), memberPageQueryDTO.getPageSize());

        // 使用自定义的联表查询方法，直接获取MemberPageVO对象
        IPage<MemberInfoPageVO> pageResult = this.baseMapper.selectMemberInfoPage(page, memberPageQueryDTO);

        log.info("分页查询会员信息，条件：{}，结果：{}", memberPageQueryDTO, pageResult);
        return pageResult;
    }
}
